/*
 * Copyright (c) 2008 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.stripesbook.article.action;

import org.stripesbook.article.view.RegisterView;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StrictBinding;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.EmailTypeConverter;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;

import org.stripesbook.article.model.Database;
import org.stripesbook.article.model.User;
import org.stripesbook.rayures.action.RayuresBaseActionBean;

@UrlBinding("/register")
@StrictBinding
public class RegisterActionBean extends RayuresBaseActionBean {
  @ValidateNestedProperties({
    @Validate(field="email", required=true, converter=EmailTypeConverter.class),
    @Validate(field="username", required=true),
    @Validate(field="password", required=true,
      expression="${this eq confirmPassword}")
  })
  private User user;

  @Validate
  private String confirmPassword;

  /* Getters and setters for user and confirmPassword */

  @DontValidate
  @DefaultHandler
  public void view() {
  }

  @Override
  public Object getView() {
    return tag(RegisterView.class);
  }

  public Resolution register() {
    // Set random activation code; we'll just hardcode a value for this example
    user.setActivationCode("A1B2");

    // Save the user to the "database"
    Database.addUser(user);

    return new RedirectResolution(ActivateActionBean.class)
      .addParameter("user.username", user.getUsername());
  }

  @ValidationMethod
  public void validateUniqueUsername(ValidationErrors errors) {
    if (Database.userExists(user.getUsername())) {
      errors.add("user.username",
        new SimpleError("The username is already taken"));
    }
  }

  public User getUser() {
    return user;
  }
  public void setUser(User user) {
    this.user = user;
  }
  public String getConfirmPassword() {
    return confirmPassword;
  }
  public void setConfirmPassword(String confirmPassword) {
    this.confirmPassword = confirmPassword;
  }
}
